<?php

class Thanh_toan extends Admin_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("Quanly_m");
        $this->_data['sidebar'] = "thanh_toan";
        //$this->load->library('formdate');
    }

    public function index() {
        $this->danh_sach();
    }

    public function danh_sach() {
        is_admin();
        $this->_data['page_title'] = "danh sach thanh toan";
        $sale_id = $this->session->userdata('admin_id');
        $perpage = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($perpage == "") {
            $perpage = 10; //10 records per page
        }
        if ($offset == "") {
            $offset = "";
        }
        $config['base_url'] = base_url() . "index.php/thanh_toan/danh_sach/" . $perpage . "/";
        $config['per_page'] = $perpage;
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Quanly_m->_get_customer_by_sale_id($sale_id, 0, 0, TRUE);
        if ($offset > 0 && $offset == $config['total_rows']) {
            $offset = $offset - $perpage;
        }
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $config['total_rows'];
        $this->_data['perpage'] = $perpage;
        $this->_data['offset'] = $offset;

        $query = $this->Quanly_m->_get_customer_by_sale_id($sale_id, $perpage, $offset, FALSE);
        $this->_data['thanhtoan_data'] = $query;
        $this->display_admin("admin/quanly/danh_sach_thanh_toan");
    }

    function thong_tin_thanh_toan($id) {
        is_admin();
        $this->_data['page_title'] = "Thong tin thanh toan cua khach hang";
        $thanhtoan_data = $this->Quanly_m->_get_pay_infor_by_customer($id);
        $khach_hang_data = $this->Quanly_m->_get_data_by_id('khachhang', $id);
        $this->_data['data'] = $thanhtoan_data;
        $this->_data['khach_hang'] = $khach_hang_data;
        $this->display_admin("admin/quanly/thong_tin_thanh_toan");
    }

    function thanh_toan_dich_vu($id_dichvu) {
        is_admin();
        $this->_data['page_title'] = "Thong tin thanh toan cua khach hang";
        $thanhtoan_data = $this->Quanly_m->_get_pay_infor_by_service($id_dichvu);
        $khach_hang_data = $this->Quanly_m->_get_customer_by_service($id_dichvu);
        $dich_vu = $this->Quanly_m->_get_service_data_by_id($id_dichvu);
        $this->_data['data'] = $thanhtoan_data;
        $this->_data['khach_hang'] = $khach_hang_data;
        $this->_data['dich_vu'] = $dich_vu;
        $this->display_admin("admin/quanly/thong_tin_khach_hang_thanh_toan");
    }

    function cap_nhat_thanh_toan($id) {
        is_admin();
        $this->_data['page_title'] = "cap nhat thong tin thanh toan cua khach hang";
        $thanh_toan_data = $this->Quanly_m->_get_data_by_id('thanhtoan', $id);
        $this->_data['data'] = $thanh_toan_data;
        if ($this->input->post('submit')) {
            $config = array(
                array("field" => "so_tien",
                    "label" => "Số tiền",
                    "rules" => "required|trim|xss_clean"
                )
            );
            $this->form_validation->set_rules($config); //set validation rules

            if ($this->form_validation->run() === TRUE) {
                $pay_date = $this->input->post('pay_year') . '-' . $this->input->post('pay_month') . '-' . $this->input->post('pay_date') . ' ' . date('H:i', time());
                $pay_long = human_to_unix($pay_date);

                $data_thanhtoan = array(
                    'ngay_tra_tien' => $pay_long,
                    'so_tien' => $this->input->post('so_tien'),
                    'chi_tiet' => $this->input->post('chi_tiet'),
                    'tinh_trang_tra_tien' => $this->input->post('tinh_trang_tra_tien')
                );

                $query = $this->Quanly_m->update_data('thanhtoan', $data_thanhtoan, $id);
                $cus_id = $this->Quanly_m->_get_customer_id_by_pay($id);
                if ($query === TRUE) {
                    $this->session->set_flashdata('error', 'Cập nhật thành công');
                    redirect('thanh_toan/thong_tin_thanh_toan/' . $cus_id->id_khach_hang);
                } else {
                    $this->session->set_flashdata('error', 'Cập nhật thất bại');
                    redirect('thanh_toan/thong_tin_thanh_toan/' . $cus_id->id_khach_hang);
                }
            } else {
                $this->display_admin("admin/quanly/sua_thanh_toan");
            }
        }
        $this->display_admin("admin/quanly/sua_thanh_toan");
    }

    function them_thanh_toan($service_id) {
        is_admin();
        $this->_data['page_title'] = "cap nhat thong tin thanh toan cua khach hang";
        $thanhtoan_data = $this->Quanly_m->_get_pay_infor_by_service($service_id);
        $khach_hang_data = $this->Quanly_m->_get_customer_by_service($service_id);
        $dich_vu = $this->Quanly_m->_get_service_data_by_id($service_id);
        $thanh_toan_id = $this->Quanly_m->_get_pay_infor($service_id)->id;
        $this->_data['data'] = $thanhtoan_data;
        $this->_data['khach_hang'] = $khach_hang_data;
        $this->_data['dich_vu'] = $dich_vu;


        if ($this->input->post('submit')) {
            $config = array(
                array("field" => "so_tien",
                    "label" => "Số tiền",
                    "rules" => "required|trim|xss_clean"
                )
            );
            $this->form_validation->set_rules($config); //set validation rules

            if ($this->form_validation->run() === TRUE) {
                $pay_date = $this->input->post('pay_year') . '-' . $this->input->post('pay_month') . '-' . $this->input->post('pay_date') . ' ' . date('H:i', time());
                $pay_long = human_to_unix($pay_date);

                $data_thanhtoan = array(
                    'ngay_thanh_toan' => $pay_long,
                    'so_tien' => $this->input->post('so_tien'),
                    'ghi_chu' => $this->input->post('chi_tiet'),
                    'id_thanh_toan' => $thanh_toan_id
                );

                $query = $this->Quanly_m->insert_data('dot_thanh_toan', $data_thanhtoan);

                if ($query === TRUE) {


                    $this->session->set_flashdata('error', 'Cập nhật thành công');
                    redirect('thanh_toan/thanh_toan_dich_vu/' . $service_id);
                } else {
                    $this->session->set_flashdata('error', 'Cập nhật thất bại');
                    redirect('thanh_toan/thanh_toan_dich_vu/' . $service_id);
                }
            } else {
                $this->display_admin("admin/quanly/thong_tin_khach_hang_thanh_toan");
            }
        }
        $this->display_admin("admin/quanly/thong_tin_khach_hang_thanh_toan");
    }

    function xoa_dot_thanh_toan($pay_period_id, $service_id, $pay_id) {
        is_admin();
        $query = $this->Quanly_m->delete_pay_period($pay_period_id);
        $service_id = $this->uri->segment(4);
        $pay_id = $this->uri->segment(5);
        if ($query === TRUE) {

            $this->session->set_flashdata('error', 'Xóa thành công');
            redirect('thanh_toan/thanh_toan_dich_vu/' . $service_id);
        }
    }

//    function update_pay_status($pay_id, $service_id) {
//
//        if ($this->check_pay_status($pay_id, $service_id) == TRUE) {
//            $data = array('tinh_trang' => '1');
//
//            $query = $this->Quanly_m->update_data('thanhtoan', $data, $pay_id);
//        } else {
//            $data = array('tinh_trang' => '0');
//
//            $query = $this->Quanly_m->update_data('thanhtoan', $data, $pay_id);
//        }
//    }
//
//    function check_pay_status($pay_id, $service_id) {
//        $pay_infor = $this->Quanly_m->_get_pay_infor($service_id);
//        $money = $pay_infor->so_tien;
//
//        $pay_period_total_money = $this->Quanly_m->_get_pay_period_money($pay_id);
//        foreach ($pay_period_total_money as $value) {
//
//            if ($value['so_tien'] >= $money) {
//                return TRUE;
//            } else {
//                return FALSE;
//            }
//        }
//    }

    function danh_sach_chua_thanh_toan() {
        is_admin();

        if ($this->session->userdata('admin_role') == '1') {
            
            $data = $this->Quanly_m->_get_pay_not_done(TRUE);
            $this->_data['data'] = $data;
        } else {
            $sale_id = $this->session->userdata('admin_id');
            $data = $this->Quanly_m->_get_pay_not_done($sale_id, FALSE);
            $this->_data['data'] = $data;
        }

        $this->display_admin('admin/quanly/danh_sach_khach_hang_chua_thanh_toan');
    }

    function doi_trang_thai($pay_id) {
        if ($this->check_trang_thai($pay_id) === TRUE) {
            $data = array('tinh_trang' => '0');
            $query = $this->Quanly_m->update_data('thanhtoan', $data, $pay_id);
        } else {
            $data = array('tinh_trang' => '1');
            $query = $this->Quanly_m->update_data('thanhtoan', $data, $pay_id);
        }
        $khach_hang_id = $this->uri->segment(4);
        redirect('khach_hang/thong_tin_khach_hang/' . $khach_hang_id);
    }

    function check_trang_thai($pay_id) {
        $query = $this->Quanly_m->_get_data_by_id('thanhtoan', $pay_id);
        if ($query->tinh_trang == 1) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

}